/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Label;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author BIM1LA
 */
public class JColorBar extends JPanel {

    int offset = 25;
    double lowerb, upperb;
    double interv;
    double shift;
    final Color[] colorbar = new Color[]{
        
        new Color(0xB400BF),new Color(0x8700CF),  new Color(0x5A00DF), new Color(0x2D00EF),
        new Color(0x0000FF), new Color(0x0017FF), new Color(0x002EFF), new Color(0x0046FF),
        new Color(0x005DFF), new Color(0x0074FF), new Color(0x008BFF), new Color(0x00A2FF), new Color(0x00B9FF), new Color(0x00D1FF), new Color(0x00E8FF),
        new Color(0x00FFFF), new Color(0x00FFE8), new Color(0x00FFD1), new Color(0x00FFB9),
        new Color(0x00FFA2), new Color(0x00FF8B), new Color(0x00FF74),
        new Color(0x00FF5D), new Color(0x00FF46), new Color(0x00FF2E), new Color(0x00FF17),
        new Color(0x00FF00), new Color(0x17FF00), new Color(0x2EFF00), new Color(0x46FF00),
        new Color(0x5DFF00), new Color(0x74FF00), new Color(0x8BFF00),
        new Color(0xA2FF00), new Color(0xB9FF00), new Color(0xD1FF00),
        new Color(0xE8FF00), new Color(0xFFFF00), new Color(0xFFE800), new Color(0xFFD100),
        new Color(0xFFB900), new Color(0xFFA200), new Color(0xFF8B00),
        new Color(0xFF7400), new Color(0xFF5D00), new Color(0xFF4600),
        new Color(0xFF2E00), new Color(0xFF1700), new Color(0xFF0000)
    };
    
     NumberFormat formatter = new DecimalFormat( "0.###E0" );

    public JColorBar() {


this.lowerb = 0;
this.upperb =  1;
this.interv = 1;
this.shift = 0;


        setPreferredSize(new Dimension(0, 100));

        setBackground(Color.white);
    }


    

    public void paintComponent(Graphics g) {

        super.paintComponents(g);

        g.drawLine(offset, offset, offset, this.getHeight() - offset);
        g.drawLine(offset, offset, this.getWidth() - offset, offset);
        g.drawLine(offset, this.getHeight() - offset, this.getWidth() - offset, this.getHeight() - offset);
        g.drawLine(this.getWidth() - offset, offset, this.getWidth() - offset, this.getHeight() - offset);



        int o = offset + 1;
        int u = this.getHeight() - 2 * offset - 1;
        int l = offset + 1;
        int dx = (this.getWidth() - 2 * (offset + 1)) / colorbar.length;
        int mod = (this.getWidth() - 2 * (offset + 1)) % colorbar.length;
      
        for (int i = 0; i < colorbar.length; i++) {

            if (mod >= 0) {
                dx += 1;
            }

            g.setColor(colorbar[i]);
            g.fillRect(l, o, dx, u);

            l = l + dx;
            if (mod >= 0) {
                mod--;
                dx -= 1;
            }
        }
        g.setColor(Color.black);
        g.drawString(formatter.format(lowerb), offset, this.getHeight()-10);
        g.drawString(formatter.format(upperb), this.getWidth()-offset-20, this.getHeight()-10);
       
        for (int i = 1; i < 4; i++) {
//            System.out.println(lowerb+(double)i/4.*(upperb-lowerb));
            
             g.drawString(formatter.format(lowerb+(double)i/4.*(upperb-lowerb)), (this.getWidth()-offset)*i/4-20, this.getHeight()-10);
            
        }
    }
    
    public void setulBound(double lowerb, double upperb){
    
    this.lowerb = lowerb;
    this.upperb = upperb;
    this.shift = -lowerb;
    this.interv = upperb-lowerb;
    
    }
    
    public double getlowerb(){return lowerb;}
    public double getupperb(){return upperb;}
    
    
    public Color getColor(double h){
    
    if(h<lowerb||h>upperb)return null;
    
    else{
    
        int colornumber = (int)((h+shift)/interv*colorbar.length);
        
        if(colornumber==colorbar.length) {
        
        colornumber--;
          //  System.out.println("colornumber--");
        
        }
        
        return colorbar[colornumber];
    
    
    }
    
    
    }
    
    
    
    
    
    
    
    
    

}
